package com.lovestudying.dao.Impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import java.util.Map.Entry;

import com.lovestudying.dao.LikelistDao;
import com.lovestudying.entity.Like;
import com.lovestudying.util.DBUtil_Pro;
import com.lovestudying.util.PageInfo;

public class LikelistDaoImpl implements LikelistDao {

	@Override
	public Integer DeleteLike(Map<String, Object> Where_Param) {
		// 创建sql语句
		StringBuffer sql = new StringBuffer("DELETE FROM Like_list");
		// Where参数列表
		Vector<Object> params = new Vector<>();
		// 根据Map中的情况构建 查询语句的条件
		if (Where_Param != null && Where_Param.size() != 0) {
			sql.append(" WHERE");
			int i = 0;
			for (Entry<String, Object> element : Where_Param.entrySet()) {
				sql.append(" " + element.getKey() + " = ?");
				params.add(element.getValue());
				i++;
				if (i < Where_Param.size()) {
					sql.append(" and");
				}
			}
		}

		return DBUtil_Pro.doUpdate(sql.toString(), params);
	}

	@Override
	public Integer InsertLike(Map<String, Object> Value_Param) {
		// 创建sql语句
		StringBuffer sql = new StringBuffer("INSERT INTO  Like_list (");
		// values参数列表
		Vector<Object> params = new Vector<>();
		int i = 0;
		// 根据Map中的情况构建 Insert语句的条件
		for (Entry<String, Object> element : Value_Param.entrySet()) {
			sql.append(element.getKey());
			params.add(element.getValue());
			i++;
			if (i < Value_Param.size()) {
				sql.append(",");
			}
		}
		sql.append(") VALUES (");
		for (int j = 0; j < params.size(); j++) {
			sql.append("?");
			if (j < params.size() - 1) {
				sql.append(",");
			}
		}
		sql.append(")");
		return DBUtil_Pro.doUpdate(sql.toString(), params);
	}

	@Override
	public Integer UpdateLike(Map<String, Object> Set_Param, Map<String, Object> Where_Param) {
		// 创建sql语句
		StringBuffer sql = new StringBuffer("UPDATE Like_list SET");
		// 参数列表
		Vector<Object> params = new Vector<>();

		int i = 0;
		// 根据Map中的情况构建 Insert语句的条件
		for (Entry<String, Object> element : Set_Param.entrySet()) {
			sql.append(" " + element.getKey() + " = ?");
			params.add(element.getValue());
			i++;
			if (i < Set_Param.size()) {
				sql.append(",");
			}
		}
		sql.append(" WHERE");
		int j = 0;
		for (Entry<String, Object> element : Where_Param.entrySet()) {
			sql.append(" " + element.getKey() + " = ?");
			params.add(element.getValue());
			j++;
			if (j < Where_Param.size()) {
				sql.append(" and");
			}
		}
		return DBUtil_Pro.doUpdate(sql.toString(), params);
	}

	@Override
	public PageInfo<Like> findLikeUniAccount(Map<String, Object> Where_Param,
			Map<String, Object> OrdeBy_Param, Integer Lmt_pageSize, Integer Lmt_pageNum) {
		List<Like> list = new Vector<>();
		PageInfo<Like> LikePage;
		ResultSet rs = null;
		// 创建sql语句
		StringBuffer sql = new StringBuffer(
				"SELECT *  FROM Like_list like_list INNER JOIN Account account ON account.account_id = like_list.account_id");
		// Where参数列表
		Vector<Object> params = new Vector<>();
		// 根据Map中的情况构建 查询语句的条件
		if (Where_Param != null && Where_Param.size() != 0) {
			sql.append(" WHERE");
			int i = 0;
			for (Entry<String, Object> element : Where_Param.entrySet()) {
				sql.append(" " + element.getKey() + " = ?");
				params.add(element.getValue());
				i++;
				if (i < Where_Param.size()) {
					sql.append(" and");
				}
			}
		}
		// System.out.println(sql.toString());
//		System.out.println(params);
		int total = 0;
		try {
			ResultSet last = DBUtil_Pro.doQuery(sql.toString(), params);
			last.last();
			total = last.getRow();
			// System.out.println(count);
		} catch (SQLException e1) {
			// TODO 自动生成的 catch 块
			e1.printStackTrace();
		}

		// 添加OrderBy参数
		if (OrdeBy_Param != null && OrdeBy_Param.size() != 0) {
			sql.append(" ORDER BY");
			int i = 0;
			// 遍历Map
			for (Entry<String, Object> element : OrdeBy_Param.entrySet()) {
				sql.append(" " + element.getKey());
				// 做升降序设置，键的值是null则是默认的ASC，不为null则添加DESC
				if (element.getValue() != null) {
					sql.append(" DESC");
				}
				i++;
				if (i < OrdeBy_Param.size()) {
					sql.append(",");
				}
			}
		}
		// 添加Limit参数
		if (Lmt_pageNum != null && Lmt_pageSize != null) {
			sql.append(" LIMIT ?,?");
			params.add((Lmt_pageNum - 1) * Lmt_pageSize);
			params.add(Lmt_pageSize);
		}

		// 执行SQL语句
		// System.out.println(sql.toString());
		rs = DBUtil_Pro.doQuery(sql.toString(), params);
		try {
			while (rs.next()) {
				Like Like = new Like(rs.getString("object_id"), rs.getString("like_time"),
						rs.getString("account_id"));
				list.add(Like);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

		LikePage = new PageInfo<>(list, Lmt_pageNum, Lmt_pageSize, total);
		return LikePage;
	}

}
